package com.stox.charting.ui.swing.model.uiUnit;

import java.awt.Color;

import com.stox.charting.ui.swing.model.ChartModel;

public abstract class AbstractUiUnit<T> extends AbstractRectangle implements UiUnit {

	private final ChartModel<T> model;
	private final T value;
	private final int index;
	private Color color;

	public AbstractUiUnit(ChartModel<T> model, int index) {
		this.model = model;
		this.index = index;
		this.value = model.getData().get(index);
	}
	
	@Override
	public ChartModel<T> getModel() {
		return model;
	}

	@Override
	public int getIndex() {
		return index;
	}
	
	public T getValue() {
		return value;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + index;
		result = prime * result + ((model == null) ? 0 : model.hashCode());
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractUiUnit<T> other = (AbstractUiUnit<T>) obj;
		if (index != other.index)
			return false;
		if (model == null) {
			if (other.model != null)
				return false;
		} else if (!model.equals(other.model))
			return false;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		return true;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

}
